﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;

namespace KPIS.GERP.GBMS.DAL
{
    public sealed class OptionTypeDAL : IOptionTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public OptionTypeDAL()
        {

        }

        public int Insert(OptionTypeInfo optInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@name", optInfo.Name);
                dbManager.AddParameters(1, "@lbname", optInfo.LabelName);
                dbManager.AddParameters(2, "@child", optInfo.HasChild);
                dbManager.AddParameters(3, "@descr", optInfo.HasDescription);
                dbManager.AddParameters(4, "@jobsite", optInfo.HasJobSite);

                strSQL = "INSERT INTO BGM_SYSTEM_OPTION_TYPE (BGM_OPTION_TYPE_NAME, LABEL_NAME, HAS_CHILD, HAS_DESCR, HAS_JOBSITE)"
                    + " VALUES (@name, @lbname, @child, @descr, @jobsite) ";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Update(OptionTypeInfo optInfo)
        {
            string strSQL;

            try
            {
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", optInfo.Sequence);
                dbManager.AddParameters(1, "@name", optInfo.Name);
                dbManager.AddParameters(2, "@lbname", optInfo.LabelName);
                dbManager.AddParameters(3, "@child", optInfo.HasChild);
                dbManager.AddParameters(4, "@descr", optInfo.HasDescription);
                dbManager.AddParameters(5, "@jobsite", optInfo.HasJobSite);

                strSQL =  "UPDATE BGM_SYSTEM_OPTION_TYPE"
                + " SET BGM_OPTION_TYPE_NAME = @name"
                    + ", LABEL_NAME = @lbname"
                    + ", HAS_CHILD = @child"
                    + ", HAS_DESCRIPTION = @descr"
                    + ", HAS_JOBSITE = @jobsite"
                + " WHERE BGM_OPTION_TYPE_SEQ = @seq";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public int Delete(int seq)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@status", SystemInfo._DeleteRecordStatus);

                return dbManager.ExecuteNonQuery(CommandType.Text, "UPDATE BGM_SYSTEM_OPTION_TYPE SET RECORD_STATUS = @status WHERE BGM_OPTION_TYPE_SEQ = @seq");
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public IList<OptionTypeInfo> Search(int seq)
        {
            string strSQL;

            List<OptionTypeInfo> srcList = new List<OptionTypeInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT BGM_OPTION_TYPE_SEQ, BGM_OPTION_TYPE_NAME, LABEL_NAME, HAS_CHILD, HAS_DESCRIPTION, HAS_JOBSITE"
                    + " FROM BGM_SYSTEM_OPTION_TYPE"
                    + " WHERE RECORD_STATUS not like @status"
                    + (seq > 0 ? " AND BGM_OPTION_TYPE_SEQ = @seq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    OptionTypeInfo src = new OptionTypeInfo(rdr.GetInt32(0), (rdr.IsDBNull(1)) ? "" : rdr.GetString(1), (rdr.IsDBNull(2)) ? "" : rdr.GetString(2), rdr.GetInt32(3), rdr.GetInt32(4), rdr.GetInt32(5));

                    srcList.Add(src);
                }

                return srcList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
